<?php

namespace App;
use DB;
use Illuminate\Database\Eloquent\Model;

class Msg extends Model{
    //展示数据
    function select($keyword){
        $list=DB::table('msgs')->where('title','like','%'.$keyword.'%')->paginate(3);
        return $list;
    }
    //添加数据
    function add(){
        $req=request();
        $time=time();
        $img=($req->file('img')->move('pic',$time.$_FILES['img']['name']));
        $res=DB::table('msgs')->insert(['title'=>$_POST['title'],'content'=>$_POST['content'],'img'=>$img]);
        return $res;
    }
    //修改数据
    function up($id){
        $req=request();
        $time=time();
        $img=($req->file('img')->move('pic',$time.$_FILES['img']['name']));
        $res=DB::table('msgs')->where('id',$id)->update(['title'=>$_POST['title'],'content'=>$_POST['content'],'img'=>$img]);
        return $res;
    }
    //删除留言
    function del($id){
        $res=DB::table('msgs')->where('id',$id)->delete();
        return $res;
    }
    
    //查询图片
    function show($id){
        $list=DB::table('msgs')->select('img')->where('id',$id)->first();
        return $list;
    }
}
